package com.xquant.pricing.pricingConfig.controller;

import com.xquant.common.security.annotation.RequiresPermissions;
import com.xquant.pricing.pricingConfig.service.EngineConfigService;
import com.xquant.pricing.utils.CommResponse;
import com.xquant.pricing.utils.ConfigSupport;
import com.xquant.pricing.utils.FtpUtil;
import com.xquant.common.engine.xPP.cxf.XQuantPPService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.WeakHashMap;

import static com.xquant.common.core.utils.PageUtils.startPage;

/**
 * @Auther: newCould
 * @Date: 01/03/2022 15:22
 * @Description: newCloud Controller  引擎配置
 */
@RestController
@RequestMapping("engineConfig")
public class EngineConfigController {

    @Autowired
    private EngineConfigService engineConfigService;

    @Autowired
    private XQuantPPService xQuantPPService;

    @ResponseBody
    @RequestMapping(value = "/uploadDll.action")
    public Object uploadDll(MultipartFile file) {
        String hostname = ConfigSupport.getInstance().getProperty("ftp.dll.hostname");
        Integer port = Integer.parseInt(ConfigSupport.getInstance().getProperty("ftp.dll.port"));
        String username = ConfigSupport.getInstance().getProperty("ftp.dll.username");
        String password = ConfigSupport.getInstance().getProperty("ftp.dll.password");
        String filePath = ConfigSupport.getInstance().getProperty("ftp.dll.filePath");
        FtpUtil util = new FtpUtil();
//        CommonsMultipartFile multipartFile = file.getFile();
        String fileName = file.getOriginalFilename();
        try {
            if (util.uploadFile(hostname, port, username, password, filePath, fileName, file.getInputStream())) {
                return new CommResponse(true, "上传成功");
            } else {
                return new CommResponse(false, "上传失败");
            }
        } catch (Exception ex) {
            return new CommResponse(false, "上传失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/uploadDllFile.action")
    public Object uploadDllFile(HttpServletRequest request, MultipartFile file){

        String fullname = file.getOriginalFilename();
        String type = FilenameUtils.getExtension(fullname);

        if(xQuantPPService.getLocalPath() == null){
            ClassLoader classLoader = EngineConfigController.class.getClassLoader();
            URL resource = classLoader.getResource("templates/dllUpload");
            xQuantPPService.setLocalPath(resource.getPath());
        }
        //指定DLL上传目录
        String filePath = xQuantPPService.getLocalPath() +"\\" + fullname;

        File newfile = new File(filePath);
        if(!newfile.exists()){
            newfile.mkdirs();
        }
        try {
            file.transferTo(newfile);
            xQuantPPService.getUploadDllNames().add(filePath);
            xQuantPPService.notifyUploadDll();
            return new CommResponse(true,"成功");
        } catch (IOException e) {
            e.printStackTrace();
            return new CommResponse(false,"成功");
        }

    }

    @ResponseBody
    @RequestMapping(value="/queryEngineConfigs.action")
    public Object queryEngineConfigs(HttpServletRequest request,@RequestParam Map<String,Object> params){
        startPage();
        return engineConfigService.queryEngineConfigByPage(params);
    }

    @RequiresPermissions("pricingconfig:engineconfig:add")
    @ResponseBody
    @RequestMapping(value="saveConfig.action")
    public Object saveConfig(HttpServletRequest request,@RequestParam Map<String,Object> params){

        try{
            engineConfigService.saveConfig(params);
            return new CommResponse(true);
        }catch (Exception e){
            return new CommResponse(false, e.getMessage());
        }
    }
    @RequiresPermissions("pricingconfig:engineconfig:edit")
    @ResponseBody
    @RequestMapping(value="updateConfig.action")
    public Object updateConfig(HttpServletRequest request,@RequestParam Map<String,Object> params){

        engineConfigService.updateConfig(params);
        return new CommResponse(true);
    }
    @RequiresPermissions("pricingconfig:engineconfig:remove")
    @ResponseBody
    @RequestMapping(value="deleteConfig.action")
    public Object deleteConfig(HttpServletRequest request){
        String ppCodes = request.getParameter("ppCodes");
        Map<String, Object> params = new WeakHashMap<>();
        params.put("ppCodes",ppCodes.split(","));
        engineConfigService.deleteConfigs(params);
        return new CommResponse(true);
    }

    @GetMapping("/queryContractCfgCountByPPCode.action")
    public int queryContractCfgCountByPPCode(@RequestParam String ppCode){
        return engineConfigService.queryContractCfgCountByPPCode(ppCode);
    }

}
